package cn.sdc.bugKiller.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.sdc.bugKiller.bean.BugBean;
import cn.sdc.bugKiller.bean.BugSearchBean;
import cn.sdc.bugKiller.bean.BugStatus;
import cn.sdc.sfw.util.StringUtil;

public class BugDaoImpl extends BaseDao {
	/**
	 * get member by username
	 */
	public BugBean getBugById(Integer id){
		StringBuffer sqlBuffer = getSqlFromFile("searchBug.sql");
		if(sqlBuffer == null || sqlBuffer.toString().isEmpty()){
			return null;
		}
		sqlBuffer.append("and bug.id= ?/*id*/\n");
		BugBean bean = (BugBean)dbutil.doSelectBean(
						BugBean.class,
						sqlBuffer.toString(),
						new Object[]{id},
						new Class[]{Integer.class});
		return bean;
	}
	
	
	public Long countBugByStatus(Integer... status){
		StringBuffer sqlBuffer = getSqlFromFile("countBug.sql");
		if(sqlBuffer == null || sqlBuffer.toString().isEmpty()){
			return null;
		}
		if(status!=null && status.length >=1){
			sqlBuffer.append("and bug.status in (?/*bug.status*/");
			for(int i=1; i < status.length; i++){
				sqlBuffer.append(", ?/*bug.status*/");
			}
			sqlBuffer.append(")\n");
		}
		Long result = (Long)dbutil.doSelectObject(
						sqlBuffer.toString(),
						status);
		return result;
	}
	
	public List<BugBean> searchBugListByStatus(Integer... status){
		StringBuffer sqlBuffer = getSqlFromFile("searchBug.sql");
		if(sqlBuffer == null || sqlBuffer.toString().isEmpty()){
			return null;
		}
		if(status!=null && status.length >=1){
			sqlBuffer.append("and bug.status in (?/*bug.status*/");
			for(int i=1; i < status.length; i++){
				sqlBuffer.append(", ?/*bug.status*/");
			}
			sqlBuffer.append(")\n");
		}
		return (List<BugBean>)dbutil.doSelectBeanList(BugBean.class,sqlBuffer.toString(),status);
	}
	
	public List<BugBean> searchStatusBugListByPriority(Integer status,Integer limitSize,Integer... priority){
		StringBuffer sqlBuffer = getSqlFromFile("searchBug.sql");
		if(sqlBuffer == null || sqlBuffer.toString().isEmpty()){
			return null;
		}
		List<Integer> params = new ArrayList<Integer>();
		if(priority!=null && priority.length >=1){
			sqlBuffer.append("and bug.priority in (?/*bug.priority*/");
			params.add(priority[0]);
			for(int i=1; i < priority.length; i++){
				sqlBuffer.append(", ?/*bug.priority*/");
				params.add(priority[i]);
			}
			sqlBuffer.append(")\n");
		}
		if(status!=null){
			sqlBuffer.append("and bug.status = ?/*bug.status*/\n");
			params.add(status);
		}
		if(limitSize!=null){
			sqlBuffer.append("limit 0,?/*LIMIT_TO*/\n");
			params.add(limitSize);
		}
		return (List<BugBean>)dbutil.doSelectBeanList(BugBean.class,sqlBuffer.toString(),params.toArray());
	}

	public List<BugBean> searchBugListByPriority(Integer... priority){
		return searchBugListByPriority(null,priority);
	}
	
	public List<BugBean> searchBugListByPriority(Integer limitSize,Integer... priority){
		return searchStatusBugListByPriority(null,limitSize,priority);
	}
	
	public List<BugBean> searchNewlyBugList(Integer newlySize){
		StringBuffer sqlBuffer = getSqlFromFile("searchBug.sql");
		if(sqlBuffer == null || sqlBuffer.toString().isEmpty()){
			return null;
		}
		if(newlySize!=null){
			sqlBuffer.append("order by bug.create_date desc\n");
			sqlBuffer.append("limit 0,?/*LIMIT_TO*/\n");
		}
		return (List<BugBean>)dbutil.doSelectBeanList(BugBean.class,sqlBuffer.toString(),new Integer[]{newlySize});
	}
	
	
	public int delBugById(Integer id){
		return deleteRecodeById(BugBean.TBL_NAME, id);
	}
	
	public BugBean updateBug(BugBean bean){
		BugBean bugBean = null; 
		String sql = getSqlFromFile("updateBug.sql").toString();
		if(sql == null || sql.isEmpty()){
			return null;
		}
		List<Object> params = new ArrayList<Object>();
		List<Class> paramtypes = new ArrayList<Class>();
		//title
		params.add(bean.getTitle());
		paramtypes.add(String.class);
		//project
		params.add(bean.getProject());
		paramtypes.add(Integer.class);
		//module
		params.add(bean.getModule());
		paramtypes.add(Integer.class);
		//program
		params.add(bean.getProgram());
		paramtypes.add(Integer.class);
		//priotiry
		params.add(bean.getPriority());
		paramtypes.add(Integer.class);
		//bugCategroy
		params.add(bean.getBugCategroy());
		paramtypes.add(Integer.class);
		//assigner
		params.add(bean.getAssigner());
		paramtypes.add(Integer.class);
		//handler
		params.add(bean.getHandler());
		paramtypes.add(Integer.class);
		//bugContent
		params.add(bean.getBugContent());
		paramtypes.add(String.class);
		//bugReply
		params.add(bean.getBugReply());
		paramtypes.add(String.class);
		//memo
		params.add(bean.getMemo());
		paramtypes.add(String.class);
		//status
		params.add(bean.getStatus());
		paramtypes.add(Integer.class);
		//createDate
		params.add(new Date());
		paramtypes.add(Date.class);
		//id
		params.add(bean.getId());
		paramtypes.add(Integer.class);
		
		int result = dbutil.doUpdate(sql,params.toArray(),paramtypes.toArray(new Class[]{}));

		if(result == 1){
			bugBean = bean;
		}
		return bugBean;
	}
	
	public BugBean insertBug(BugBean bean){
		BugBean bugBean = null; 
		String sql = getSqlFromFile("insertBug.sql").toString();
		if(sql == null || sql.isEmpty()){
			return null;
		}
		List<Object> params = new ArrayList();
		List<Class> paramtypes = new ArrayList<Class>();
		//title
		params.add(bean.getTitle());
		paramtypes.add(String.class);
		//project
		params.add(bean.getProject());
		paramtypes.add(Integer.class);
		//module
		params.add(bean.getModule());
		paramtypes.add(Integer.class);
		//program
		params.add(bean.getProgram());
		paramtypes.add(Integer.class);
		//priotiry
		params.add(bean.getPriority());
		paramtypes.add(Integer.class);
		//bugCategroy
		params.add(bean.getBugCategroy());
		paramtypes.add(Integer.class);
		//assigner
		params.add(bean.getAssigner());
		paramtypes.add(Integer.class);
		//handler
		params.add(bean.getHandler());
		paramtypes.add(Integer.class);
		//bugContent
		params.add(bean.getBugContent());
		paramtypes.add(String.class);
		//bugReply
		params.add(bean.getBugReply());
		paramtypes.add(String.class);
		//memo
		params.add(bean.getMemo());
		paramtypes.add(String.class);
		//status
		params.add(BugStatus.STATUS_UNFIX);
		paramtypes.add(Integer.class);
		//createDate
		params.add(new Date());
		paramtypes.add(Date.class);
		
		int result = dbutil.doUpdate(sql,params.toArray(),paramtypes.toArray(new Class[]{}));

		if(result == 1){
			sql = "select LAST_INSERT_ID()";
			Long lastId = (Long)dbutil.doSelectObject(sql);
			bugBean = getBugById(new Integer(lastId.intValue()));
		}
		return bugBean;
	}
	
	public List<BugBean> searchBug(BugSearchBean searchBean, String... orderby){
		return searchBug(searchBean,StringUtil.EMPTY_STRING,null,null,orderby);
	}
	
	public List<BugBean> searchBug(BugSearchBean searchBean,String appendSql, Object[] args, Class[] argTypes, String... orderby){
		StringBuffer sqlBuffer = getSqlFromFile("searchCondictionBug.sql");
		if(sqlBuffer == null || sqlBuffer.toString().isEmpty()){
			return null;
		}else if(!appendSql.isEmpty()){
			sqlBuffer = sqlBuffer.append(appendSql);
		}
		if(orderby!=null && orderby.length >=1){
			sqlBuffer.append("order by ").append(orderby[0]);
			for(int i=1; i < orderby.length; i++){
				sqlBuffer.append(orderby[i]);
			}
		}
		Object[] params = new Object[18];
		params[0] = params[1] = searchBean.getModule();
		params[2] = params[3] = searchBean.getProgram();
		params[4] = params[5] = searchBean.getPriority();
		params[6] = params[7] = searchBean.getBugCategroy();
		params[8] = params[9] = searchBean.getAssigner();
		params[10] = params[11] = searchBean.getHandler();
		params[12] = params[13] = searchBean.getStatus();
		params[14] = params[15] =
		params[16] = params[17] = searchBean.getSearchContent();
		
		Class[] paramtypes = new Class[18];
		paramtypes[0] = paramtypes[1] = 
		paramtypes[2] = paramtypes[3] = 
		paramtypes[4] = paramtypes[5] = 
		paramtypes[6] = paramtypes[7] = 
		paramtypes[8] = paramtypes[9] = 
		paramtypes[10] = paramtypes[11] =
		paramtypes[12] = paramtypes[13] = Integer.class;
		paramtypes[14] = paramtypes[15] = 
		paramtypes[16] = paramtypes[17] = String.class;
		
		List<BugBean> beanList = (List<BugBean>)dbutil.doSelectBeanList(BugBean.class,sqlBuffer.toString(),params,paramtypes);

		return beanList;
	}
}
